//链表的模拟实现——第二遍
public class MySingleList {
    public class Node{
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    public void createList(){
        Node node1 = new Node(7);
        Node node2 = new Node(76);
        Node node3 = new Node(9);
        node1.next = node2;
        node2.next = node3;
        head = node1;
    }

    public void display() {
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size(){
        if(head == null){
            return 0;
        }
        int count = 0;
        Node cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        if(head == null){
            head = node;
        }
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if(head == null){
            head = node;
        }
        //要找到最后一个节点，从而进行尾插
        Node cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new IndexOutOfBoundException("index 下标不合法，请重新检查下标的合法性！");
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndex(index);

        if(index == 0){
            addFirst(data);
        }
        if(index == size()){
            addLast(data);
        }

        Node node = new Node(data);
        //找 index-1 对应的节点：
        Node cur = head;
        for (int i = 0; i < index-1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;

    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    private Node prevCur(int key){


        Node cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head.val == key){
            head = head.next;
        }
        Node preCur = prevCur(key);
        Node cur = preCur.next;
        preCur.next = cur.next;

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node preCur = head;
        Node cur = head.next;
        while(cur != null){
            if(cur.val == key){
                preCur.next = cur.next;
            }else{
                preCur = cur;
            }
            cur = cur.next;
        }
        if(head.val == key){
            head = head.next;
        }
    }

    //反转一个单链表
    public Node reverseList(){
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }

        Node cur = head.next;
        head.next = null;
        Node curNext = null;
        while(cur != null){
            curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //返回链表的中间节点，如果有两个中间节点，则返回第二个
    public Node midNode(){
        Node fast = head;
        Node slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //输入一个链表，输出该链表中倒数第k个结点。
    public int lastKNode(int k ){
        Node fast = head;
        Node slow = head;
        while(k - 1 > 0){
            fast = fast.next;
            k--;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    //链表是否回文
    public boolean palindrome(){
        //得到中间节点的地址：
        Node slow = midNode();
        //翻转
        Node cur = slow.next;
        Node curNext = null;
        while(cur != null){
            curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //链表从两边往中间走，比较值是否相等：
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public void clear() {
        head = null;
    }

}



